package com.yh.gulimall.search.thread;
import java.util.concurrent.*;

public class ThreadTest {
    public static ExecutorService service = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main---start");
        //方法一：
//        new Thread01().start();

        //方法二：
//        new Thread(new Runnable02()).start();

        //方法三：
//        FutureTask<Integer> futureTask = new FutureTask<>(new Callable03());
//        new Thread(futureTask).start();
//        //拿到返回结果-》等整个线程会执行完成
//        Integer integer = futureTask.get();

        //方法四：(我们使用线程池来完成我们的业务)
        //最好是系统中池只有一两个。每个异步任务提交给线程池
        // TODO submit和execute区别在于一个有返回值一个没有返回值
        service.execute(new Runnable02());

        System.out.println("main---end");
    }















    public static class Callable03 implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程"+Thread.currentThread().getName());
            int i=10/2;
            System.out.println("运行结果"+i);
            return i;
        }
    }

    public static class Runnable02 implements Runnable{

        @Override
        public void run() {
            System.out.println("当前线程"+Thread.currentThread().getName());
            int i=10/2;
            System.out.println("运行结果"+i);
        }
    }

    //方法一：
    public static class Thread01 extends Thread{
        @Override
        public void run() {
            System.out.println("当前线程"+Thread.currentThread().getName());
            int i=10/2;
            System.out.println("运行结果"+i);
        }
    }
}
